--- Created by Kolacats & zombekillah
--- All rights reserved.
---
--- Simple to use API.
---

OS_VERSION = "1.0.1" -- OS Version
OS_RELEASE = "Alpha" -- OS release type
MODEM_ATTACHED = ""

-----------------------
------ SETTINGS -------
-----------------------

DOWNLOAD_SERVER_ID = 42
MODEM_TEST_SERVER_ID = 63


-----------------------
--- End of Settings ---
-----------------------
lpe = true -- The loop


--
-- Runs first time setup
--
function firstTimeSetup() 
	print("CCOS needs to download some required files.")
	print("Preparing..")
	downloadFiles()
	write("As this is your first time,")
	print(" please enter some details...")
	sleep(3)
	cls() 
	write("First name: ") 
	fname = read()
	write("Second name: ")
	sname = read()
	h = fs.open("UserInfo", "w")
	h.writeLine(fname)
	h.writeLine(sname)
	-- Note how h.close() hasn't been called yet
	cls() 
	print("Welcome "..fname.." "..sname)
	sleep(2) 
	cls() 
	write("What year were you born: ")
	bdate = read()
	age = 2013 - bdate 
	write("Oh so you are "..age.." ?") 
	term.setCursorPos(1,3) 
	i = read()
	
	if(i == "yes") then
		print("I thought so...")
		else 
		age = age - 1 
		print("Ah! so you must be "..age) 
	end 
	
	h.writeLine(age) 
	h.close() 
	sleep(3)
	cls() 

end

--
-- Open all rednet sides
--
function openRednet()
	rednet.open("left")
	rednet.open("right")
	rednet.open("bottom")
	rednet.open("top")
	rednet.open("back")
end

--
-- Close all rednet sides
--
function closeRednet()
	rednet.close("left")
	rednet.close("right")
	rednet.close("bottom")
	rednet.close("back")
	rednet.close("top")
end

--
-- Check if modem is attached
--
function isModem()
	openRednet()
	rednet.send(MODEM_TEST_SERVER_ID, "c")
	id, message = rednet.receive(1)
	closeRednet()
	if(message == null)  then
		return false
	else
		return true
	end

end

---
--- Download files
---
function download(url)
	print("Downloading " ..url .."...")
	openRednet()
	rednet.send(DOWNLOAD_SERVER_ID, url, true)
	a, messagep = rednet.receive(1)
	if(messagep == null) then
		print("Could not connect to download server. Check you have the right settings.")
		shutdown()
	else
		h = fs.open(url, "w")
		h.write(messagep)
		h.close()
		print("Downloaded " ..url)
	end
	closeRednet()
end


--
-- Download required files
--
function downloadFiles()
	if(isModem()) then
		download("browser")
		download("install")
	else
		print("Cannot download required files, please connect this device to a Modem.")
		shutdown()
	end
end

--
-- Clears the screen
--
function cls()
	term.clear()
	term.setCursorPos(1,1) 
end 

--
-- Shutdown with some text
--
function shutdown()
		print("")
		print("CCOS will shut down in..")
		wait("5")
		os.shutdown()

end

---
--- Sleep Function
---
function wait(sec)
	for tme = 0, sec do
		print(sec - tme)
		sleep(1)
	end
end
--
-- Returns the First Name of the user
--
function getFirstName() 
	h = fs.open("UserInfo", "r") 
	fnme = h.readLine() 
	snme = h.readLine() 
	uage = h.readLine() 
	h.close()
		return fnme 
end 


--
-- Returns the Last Name of the user
--
function getLastName()
	h = fs.open("UserInfo", "r") 
	fnme = h.readLine() 
	snme = h.readLine()
	uage = h.readLine()
	h.close() 
		return snme 
end

--
-- Returns age of the user
--
function getAge()
	h = fs.open("UserInfo", "r")
	fnme = h.readLine()
	snme = h.readLine()
	uage = h.readLine()
	h.close() 
		return uage 
end 


-- *****************
-- The main program
-- *****************

function main()
	cls()
	modm = isModem()
	if(modm == true) then
		MODEM_ATTACHED = "Yes"
	else
		MODEM_ATTACHED = "No"
	end
	write("Welcome to CCOS V" .. OS_VERSION .. " " .. OS_RELEASE)
	print("")
	print("Modem attached? " ..MODEM_ATTACHED)
	sleep(3)
	cls() 
	
	if(fs.exists("UserInfo"))then 
		firstnme = getFirstName()
		surnme = getLastName()
		userage = getAge()
		print("Welcome " ..firstnme .." " ..surnme .."   Age: "..userage) 
		sleep(2) 
		cls() 
	else 
	firstTimeSetup()
	end 
	--
	-- LOOOP BEGINS
	--
	print("So what do you want to do ?")
	print("TIP you can type exit to get out!")
	while lpe == true do 
	
		stuff = read()
		
		if(stuff == "eat") then 
			cls() 
			print("You should go down to the Kola Cafe.") 
		else if(stuff == "exit") then 
			cls() 
			lpe = false 
		else if(stuff == "browse") then
			shell.run("browser")
		else
			cls()
			print("I do not know what you mean by "..stuff) 
		end 
		end
		end 
	end 
	--
	-- LOOP ENDS
	--
end

